﻿using System;
using System.Runtime.CompilerServices;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

namespace Nirvana
{
    [CustomEditor(typeof(AnimatorStateBehaviour))]
    public class AnimatorStateBehaviourEditor : Editor
    {
        private SerializedProperty enterActions;
        private SerializedProperty exitActions;
        private ReorderableList enterList;
        private ReorderableList exitList;

        public override void OnInspectorGUI()
        {
            base.serializedObject.Update();
            GUILayout.Space(EditorGUIUtility.singleLineHeight);
            this.enterList.DoLayoutList();
            GUILayout.Space(EditorGUIUtility.singleLineHeight);
            this.exitList.DoLayoutList();
            base.serializedObject.ApplyModifiedProperties();
        }
        private void OnEnable()
        {
            if (this.target == null) return;
            this.enterActions = serializedObject.FindProperty("enterActions");
            this.exitActions = serializedObject.FindProperty("exitActions");

            this.enterList = new ReorderableList(serializedObject, this.enterActions);
            this.enterList.drawHeaderCallback = DrawHeaderEnter;
            this.enterList.elementHeight= 2f * EditorGUIUtility.singleLineHeight;
            this.enterList.drawElementCallback = DrawElementEnter;

            this.exitList = new ReorderableList(serializedObject, this.exitActions);
            this.exitList.drawHeaderCallback = DrawHeaderExit;
            this.exitList.elementHeight = 2f * EditorGUIUtility.singleLineHeight;
            this.exitList.drawElementCallback = DrawElementExit;
        }

        private void DrawHeaderEnter(Rect rect)
        {
            GUI.Label(rect, "Enter Actions:");
        }
        private void DrawHeaderExit(Rect rect)
        {
            GUI.Label(rect, "Exit Actions:");
        }

        private void DrawElementEnter(Rect rect, int index, bool isActive, bool isFocused)
        {
            DrawElement(this.enterActions, rect, index, isActive, isFocused);
        }
        private void DrawElementExit(Rect rect, int index, bool isActive, bool isFocused)
        {
            DrawElement(this.exitActions, rect, index, isActive, isFocused);
        }
        private void DrawElement(SerializedProperty prop,Rect rect, int index, bool isActive, bool isFocused)
        {
            SerializedProperty arrayElementAtIndex = prop.GetArrayElementAtIndex(index);
            SerializedProperty eventName = arrayElementAtIndex.FindPropertyRelative("eventName");
            SerializedProperty eventParam = arrayElementAtIndex.FindPropertyRelative("eventParam");

            Rect r = new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight);
            EditorGUI.PropertyField(r, eventName, new GUIContent("Event Name:"));
            r.y += EditorGUIUtility.singleLineHeight;
            EditorGUI.PropertyField(r, eventParam, new GUIContent("Event Param:"));

        }

    }

}

